/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(getCalendarInformation())


function detectFirstWeekday(){
    
    var weekDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
   
    var firstDayInMonth = new Date(year, month-2, 1, 0, 0, 0, 0);
    var firstWeekdayInMonth = firstDayInMonth.getDay();
    return firstWeekdayInMonth;
}

function detectLeapYear(year){
    if( (year%4 == 0 && year%100 != 0) || (year%400 == 0) )
        return true;
    else
        return false;
}

function getCalendarInformation(){ 
    $.ajax({
        url: "CalendarServlet?year=2012&month=4",
        dataType: "json",
        success: function(calendarObject) {
            var calendar = eval(calendarObject)
            var tmp = "Year: "+calendar.year+"\nMonth: "+calendar.month+"\n";
                        
            tmp+="Weeks: \n";
            $.each(calendar.weeks, function(key,value){
                tmp+= "\tDays: "+value.days+'\n';
                tmp+= "\tShaded: "+value.shaded+'\n';
                tmp+= "\tEvents: \n";
                
                $.each(value.events,function(key2, value2){
                    tmp+= "\t\tName: "+value2.name+"\n";
                    tmp+= "\t\tCompleted: "+value2.completed+"\n";
                    if(value2.completed == true)
                        $("#"+(key+1)+" td:nth-child("+(key2+1)+")").attr("style","background-color: green;");
                    else
                        $("#"+(key+1)+" td:nth-child("+(key2+1)+")").attr("style","background-color: red;");
                })
                
            })
            
            alert(tmp);
        },
        error: function(data,data2) {
            alert("Failed: "+data+" \n\n"+data2);
        }
    });
    
}